package edu.mobbuzz.page.menu.inbox;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.page.menu.*;
import edu.mobbuzz.renderer.MessageRenderer;
import edu.mobbuzz.util.string.MenuString;
import java.util.Vector;


public class InboxMenu extends BaseMenu{

    private Form inboxForm;
    private Container inboxPanel;
    private Label menuTitle;
    private List list;

    public InboxMenu(){
        inboxForm = new Form();
        inboxPanel = new Container();
        menuTitle = new Label(getMenuTitle());

    }

    public void showPage(){
        Vector contactArray = new Vector();
        contactArray.addElement("A");


        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);

        inboxPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        inboxPanel.addComponent(menuTitle);
        inboxPanel.addComponent(createList(contactArray, List.VERTICAL, new MessageRenderer()));

        inboxForm.setLayout(new BorderLayout());
        inboxForm.addComponent(BorderLayout.CENTER, inboxPanel);
        inboxForm.addCommand(new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
               backToInboxMenu();
            }
        });
        inboxForm.addCommand(new Command("Share RSS") {
            public void actionPerformed(ActionEvent evt) {

            }
        });
        inboxForm.addCommand(new Command("Delete") {
            public void actionPerformed(ActionEvent evt) {

            }
        });
        inboxForm.addCommand(new Command("Open") {
            public void actionPerformed(ActionEvent evt) {
               displayMessage();
            }
        });
        inboxForm.setCommandListener(getActionlistener());
        inboxForm.setScrollable(false);
        inboxForm.show();

    }

    private List createList(Vector contacts, int orientation, ListCellRenderer renderer) {
        list = new List(contacts){
            public void keyPressed(int keyCode){
               gameActionPerformed(keyCode);
            }
        };
        list.getStyle().setBgTransparency(0);
        list.getStyle().setBorder(Border.getEmpty());
        list.setListCellRenderer(renderer);
        list.setOrientation(orientation);

        return list;
    }

    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);

        if(game == Display.GAME_FIRE){
            displayMessage();
        }else if(game == Display.GAME_DOWN){
            System.out.println("DOWN " + list.getSelectedIndex());
            if(list.getSelectedIndex()<(list.size()-1)){
               list.setSelectedIndex(list.getSelectedIndex()+1);
            }else{
                list.setSelectedIndex(0);
            }

        }else if(game == Display.GAME_UP){
             System.out.println("UP " + list.getSelectedIndex());
             if(list.getSelectedIndex()!=0){
                 list.setSelectedIndex(list.getSelectedIndex()-1);
             }else{
                 list.setSelectedIndex(list.size()-1);
             }

        }
    }
    public String getMenuTitle() {
        return MenuString.INBOX;
    }



    public void shareMessage() {

    }

    public void displayMessage() {
        
    }

    public void backToInboxMenu() {
        InboxMenu inboxMenu = new InboxMenu();
        inboxMenu.setExitCmd(getExitCmd());
        inboxMenu.setActionlistener(getActionlistener());
        inboxMenu.showPage();

    }


}
 
